package shake.util;

import java.util.HashMap;

import javax.el.ValueExpression;

import shake.annotation.Component;
import shake.el.ELs;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Component
@Singleton
public class Interpolator {
	@Inject
	ELs el;

	public Object interpolate(final String message, Object... params) {
		if (params == null) {
			params = new Object[0];
		}

		HashMap<String, Object> obj = new HashMap<String, Object>();
		obj.put("$", params);
		for (int i = 0; i < params.length; i++) {
			obj.put("$" + i, params[i]);
		}
		final Object[] output = new Object[1];
		el.eval(obj, new Clousre() {
			public void invoke() {
				ValueExpression ve = el.createValueExpression(message);
				output[0] = ve.getValue(el.createELContext());
			}
		});
		return output[0];
	}
}
